package com.android.phoenix.activity;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.phoenix.MainApplication;
import com.android.phoenix.R;
import com.android.phoenix.activity.drawLine.SportDrawLine;
import com.android.phoenix.activity.location.LocationUtils;
import com.android.phoenix.models.SportRecord;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;

public class SportRecordDetailActivity extends BaseActivity {

	private BaiduMap baiduMap;
	private MapView mapView = null;
	private OverlayOptions overlayOptions;
	private List<LatLng> listLatLongs = new ArrayList<LatLng>();
	private MapStatusUpdate mapStatusUpdate;

	private ImageView title_Imgbtn_left_up;
	private TextView title_txt_center;
	private TextView title_txt_right_up;

	private TextView tvTime;
	private TextView tvSlope;
	private TextView tvDistance;
	private TextView tvStpNum;

	private Button btn_calorie;

	private String id;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acti_sportrecorddetail);
		initView();
		initTitleBar();
	}

	private void initView() {
		id = getIntent().getExtras().getString("ID");
		mapView = (MapView) this.findViewById(R.id.acti_sport_details_bmapView);

		mapView.removeViewAt(1);
		baiduMap = mapView.getMap();

		tvTime = (TextView) this.findViewById(R.id.tvTime);
		tvSlope = (TextView) this.findViewById(R.id.tvSlope);
		tvDistance = (TextView) this.findViewById(R.id.tvDistance);
		tvStpNum = (TextView) this.findViewById(R.id.tvStpNum);

		btn_calorie = (Button) this.findViewById(R.id.btn_calorie);
		btn_calorie.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent it = new Intent(SportRecordDetailActivity.this,
						SportDrawLine.class);
				it.putExtra("path", id);
				startActivity(it);

			}
		});

		if (id == null) {
			Toast.makeText(SportRecordDetailActivity.this, "读取记录失败！",
					Toast.LENGTH_LONG).show();
			return;
		} else {
			setNum();
			drawLine();
		}

	}

	private void drawLine() {
		// path 从上面传过来
		listLatLongs = LocationUtils.getListFromFile(id);
		if (listLatLongs == null) {
			Toast.makeText(SportRecordDetailActivity.this, "读取记录失败！",
					Toast.LENGTH_LONG).show();
			return;
		}
		overlayOptions = new PolylineOptions().width(10).color(0xAAFF0000)
				.points(listLatLongs);
		baiduMap.addOverlay(overlayOptions);
		// 将中心点移动到定位点处
		// mapStatusUpdate =
		// MapStatusUpdateFactory.newLatLng(listLatLongs.get(listLatLongs.size()-1));
		mapStatusUpdate = MapStatusUpdateFactory.newLatLngZoom(
				listLatLongs.get(listLatLongs.size() - 1), 15.0f);
		baiduMap.animateMapStatus(mapStatusUpdate);
	}

	private void setNum() {
		SportRecord record = new SportRecord();
		try {
			record = MainApplication
					.getDataHelper(SportRecordDetailActivity.this)
					.getSportRecordDao().queryForId(id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		tvTime.setText(LocationUtils.countTime(
				Long.parseLong(record.getStartTime()),
				Long.parseLong(record.getEndTime())));
		tvStpNum.setText(LocationUtils.countPedaling(
				Float.parseFloat(record.getDistance()), 0f));
		tvDistance.setText(record.getDistance());

	}

	/**
	 * 
	 * @Title: initTitleBar
	 * @Description: TODO(设置统一的titleBar)
	 * @author: wWX230766
	 * @date: 2015-1-6 下午10:00:48
	 */
	private void initTitleBar() {
		title_Imgbtn_left_up = (ImageView) this
				.findViewById(R.id.title_Imgbtn_left_up);
		title_txt_center = (TextView) this.findViewById(R.id.title_txt_center);
		title_txt_center.setText(R.string.label_sport_cycle);
		title_Imgbtn_left_up.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		title_txt_right_up = (TextView) this
				.findViewById(R.id.title_txt_right_up);
		title_txt_right_up.setVisibility(View.GONE);
	}
}
